package ar.com.rys.server;


import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;

import ar.com.rys.server.entities.Person;
import ar.com.rys.server.entities.TempUserData;
import ar.com.rys.server.services.PMF;

import com.google.appengine.api.users.User;

@SuppressWarnings("serial")
public abstract class AbstractHttpServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(AbstractHttpServlet.class.getName());
	
	protected static Map<String, String> openIdProviders;
	private PersistenceManager mgr = null;
	
	protected boolean checkIsRegistredUser(User user){
		boolean returnFlag = false;
		Query query = null;
		
		try {
			mgr = getPersistenceManager();
			query = mgr.newQuery(Person.class);
			query.setFilter("email == emailParam");
			List<Person> results = (List<Person>) query.execute(user.getEmail());
			
			if(!results.isEmpty() && results.size()==1){
				returnFlag = true;
			}else {
				
				mgr.makePersistent(new TempUserData(user.getEmail()));
				
			}
			
		} finally {
			if(query != null){
				query.closeAll();
			}
			mgr.close();
		}
		return returnFlag;
	}
	
	private static PersistenceManager getPersistenceManager() {
		return PMF.get().getPersistenceManager();
	}

}
